'''
	JAJADIY工具箱
	汉中库得网络科技
	电子信箱：yyc1030@qq.com
'''
import sys
import os
import importlib
import qtmax
from pymxs import runtime as rt
from PySide2 import QtGui
from PySide2 import QtCore

# 获取根目录
__realPath = os.path.dirname(os.path.realpath(__file__))
# 判断rootPath是否在sys.path中，如果没在，将其添加进去
try:
    sys.path.index(__realPath)
except ValueError:
	sys.path.append(__realPath)
	
# 加载自定义模块
import ui, tools
importlib.reload(ui)
importlib.reload(tools)


'''
	JAJADIY-Plugin 主程序
'''
class App():
	def __init__(self):
		# 版本号
		self.version = "2023"
		# 插件根目录
		self.rootPath = os.path.dirname(os.path.realpath(__file__))
		self.logo = QtGui.QIcon(self.rootPath + "/icon/logo.png")
		self.ninePlaceGrid = tools.NinePlaceGrid()
		self.cameraUtil = tools.Camera()
		# 配置文件
		self.ini = QtCore.QSettings(self.rootPath + "/config.ini", QtCore.QSettings.IniFormat)
		self.ini.setIniCodec("utf-8")
		# 监听器
		# 经测试，unregisterRedrawViewsCallback() 方法会将所有已注册的函数都移除
		# 所以只好放在主函数中全局注册，再由各应用判断是否在重绘时做点什么
		rt.unregisterRedrawViewsCallback(self.onRedrawView)
		rt.registerRedrawViewsCallback(self.onRedrawView)

		self.mainWindow = qtmax.GetQMaxMainWindow()
		self.toolbar = ui.ToolBar(self)
		print("JAJADIY-Plugin %s已加载" % self.version)
		
		# 声明MaxScript函数，用于将BitArray转换成Array
		rt.execute("fn toArray b = (return b as Array)")
		
	# 当重绘窗口时调用
	def onRedrawView(self):
		self.ninePlaceGrid.drawNinePlaceGrid()
		self.cameraUtil.drawCameraMarks()
		
if __name__ == "__main__":
	App()